package com.gdufs.secondhandwebsite.service.impl;

import com.gdufs.secondhandwebsite.dao.CommentMapper;
import com.gdufs.secondhandwebsite.entity.Comment;
import com.gdufs.secondhandwebsite.service.CommentService;
import com.gdufs.secondhandwebsite.service.ex.UserNotLoginException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    public void createComment(Comment comment, String username, String userImg,Long postId){
        if (username == null) {
            // 是：抛出UserNotFoundException异常
            throw new UserNotLoginException("用户尚未登录");
        }
        comment.setUserName(username);
        comment.setUserImg(userImg);
        comment.setPostId(postId);
        commentMapper.CreateComment(comment);
    }
    public boolean delComment(Long commentId){
        int result = commentMapper.DeleteCommentByPrimaryKey(commentId);
        if(result>0) {
            return true;
        }else{
            return false;
        }
    }
    public boolean updateComment(Comment comment){
        int result = commentMapper.UpdateCommentSelective(comment);
        if(result>0) {
            return true;
        }else{
            return false;
        }
    }
    public List<Comment> getAllComment(){
        return  commentMapper.getAllComment();
    }
    public List<Comment> getCommentByPostId(Long postId){
        return  commentMapper.SelectCommentByPostId(postId);
    }
}
